package com.cuit;

import com.cuit.annotation.RpcService;
import com.cuit.enums.RpcErrorMessageEnum;
import com.cuit.exception.RpcException;
import io.protostuff.Rpc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Random;

/**
 * @PROJECT_NAME: rpc
 * @SCHOOL:CUIT
 * @USER: Liquid
 * @DATE: 2022/3/10 21:12
 */
@Slf4j
@RpcService(group="cuit",version="1.0",vip = false,degradeServicePath = "com.cuit.GameServiceMock")
public class GameServiceImpl implements GameService{
    static Random random;
    static {
        random = new Random();
        log.info("GameServiceImpl被创建");
    }

    @Override
    public String playGame(String message) {
        LocalDateTime start = LocalDateTime.now();
        log.info("GameServiceImpl收到：{}",message);
        log.info("处理业务逻辑");
        log.info("查询数据库");
        try {
            Thread.sleep(50+random.nextInt(100));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String info = "降级业务";
        if (!StringUtils.hasLength(message)){
            throw new RpcException(RpcErrorMessageEnum.SERVICE_INVOCATION_FAILED);
        }
        log.info("GameServiceImpl返回：{}",info);
        LocalDateTime end = LocalDateTime.now();
        log.info("花费时间：[{}]", Duration.between(start, end).toMillis());
        return info;
    }
}
